package com.redwine.app;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.adapter.MessageAddressBookAdapter;
import com.redwine.app.net.responses.MessageListResponse;
import com.redwine.app.view.CharacterParser;
import com.redwine.app.view.EmptyView;
import com.redwine.app.view.PinyinComparator;
import com.redwine.app.view.SideBar;

import java.util.ArrayList;
import java.util.Collections;


/**
 * 消息右侧通讯里
 * 作者：gassion on 17/8/29 23:47
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class MessageAddressBookActivity extends BaseActivity {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    @ViewInject(R.id.sidrbar)
    private SideBar sideBar;
    @ViewInject(R.id.country_lvcountry)
    private ListView mListView;
    @ViewInject(R.id.empty_view)
    private EmptyView emptyView;
    public static final String MESSAGE_LIST = "MESSAGE_LIST";
    private ArrayList<MessageListResponse.DataEntity> mResponseData;
    private CharacterParser characterParser;
    private PinyinComparator pinyinComparator;
    private MessageAddressBookAdapter messageAddressBookAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_address_book);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (null != intent) {
            ArrayList<MessageListResponse.DataEntity> responseData = (ArrayList<MessageListResponse.DataEntity>) intent.getSerializableExtra(MESSAGE_LIST);
            if (null != responseData && !responseData.isEmpty()) {
                this.mResponseData = responseData;
            } else {
                this.mResponseData = new ArrayList<>();
            }
        }
        headerTitle.setText(R.string.message_address_book);
        layoutBack.setVisibility(View.VISIBLE);
        refreshData();
    }

    public void refreshData() {
        characterParser = CharacterParser.getInstance();
        pinyinComparator = new PinyinComparator();
        //设置右侧触摸监听
        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                int position = messageAddressBookAdapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    mListView.setSelection(position);
                }

            }
        });
        filledData();
        Collections.sort(mResponseData, pinyinComparator);
        messageAddressBookAdapter = new MessageAddressBookAdapter(this, mResponseData);
        mListView.setAdapter(messageAddressBookAdapter);
        mListView.setEmptyView(emptyView);
        emptyView.bind();
        if (mResponseData.isEmpty()) {
            sideBar.setVisibility(View.GONE);
            setEmptyView(emptyView, null);
        } else {
            sideBar.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.layout_back})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;

        }
    }


    /**
     * 设置首字母
     *
     * @return
     */
    private ArrayList<MessageListResponse.DataEntity> filledData() {

        for (int i = 0; i < mResponseData.size(); i++) {
            MessageListResponse.DataEntity dataEntity = mResponseData.get(i);
            if (!TextUtils.isEmpty(dataEntity.getNickname())) {
                String pinyin = characterParser.getSelling(dataEntity.getNickname());
                String sortString = pinyin.substring(0, 1).toUpperCase();
                if (sortString.matches("[A-Z]")) {
                    dataEntity.setSortLetters(sortString.toUpperCase());
                } else {
                    dataEntity.setSortLetters("#");
                }
            } else {
                dataEntity.setSortLetters("#");
            }
        }
        return mResponseData;
    }
}
